#code written for R version 4.3.3 (2024-02-29) -- "Angel Food Cake"

#Load required packages
#tidyverse v2.0.0
library(tidyverse)
#ggrepel v0.9.5
library(ggrepel)
#ggthemes v5.1.0
library(ggthemes)
#MASS v7.3-60.0.1
library(MASS)
#mlogit v1.1-1
library(mlogit)

data <- read.csv("tweets_cleaned.csv")
memdata <- read.csv("original_memdata.csv")


#Creating Figures 1 and S1
frequency1 <- read.csv("safig1data.csv")
frequency2 <- read.csv("fig1data.csv")

#Figure S1 - Most Used Words as a Proportion of Partisan Vocabulary
frequency1a <- subset(frequency1, frequency1$rank_Both <= 100)

pos <- position_jitter(width = 0.001, seed = 10)

pdf(file = "figS1.pdf", width=7.25, height=7.25)
p1 <- ggplot(frequency1a, aes(x = dem_proportion, y = rep_proportion)) +
  geom_abline(color = "gray60", lty = 2) +
  geom_point(alpha = 0.8, size = 1.5, aes(color = dictionary)) +
  geom_text_repel(aes(label = feature2),
                  min.segment.length = Inf, 
                  max.overlaps = Inf,
                  box.padding = 0.1,
                  fontface = "bold",
                  force = 5) +
  scale_color_manual(values = c("blue", "gray40", "red")) +
  coord_cartesian(xlim=c(0,0.016), ylim=c(0,0.016)) +
  theme_hc() +
  theme(plot.title = element_text(hjust=0.5),
        legend.position="none",
        axis.text.x = element_text(margin = margin(t=-5)),
        axis.ticks = element_blank()) +
  labs(y = "Proportion of Republican vocabulary", 
       x = "Proportion of Democratic vocabulary") +
  ggtitle("")
p1
dev.off()


#Figure 1 - Frequency of Usage for Dictionary Terms
freq_insurrect <- subset(frequency2, frequency2$dictionary == "insurrection")
freq_protest <- subset(frequency2, frequency2$dictionary == "protest")

pdf(file = "fig1a.pdf", width=4.75, height=4.75)
fig1a <- ggplot(freq_insurrect, aes(x = reorder(feature2, frequency_Both), 
                                    y = frequency_Both)) +
  geom_point() + 
  coord_flip() +
  ylim(0,300) +
  labs(x = NULL, y = "Frequency") +
  ggtitle("Attack on Democracy Dictionary") +
  theme_hc() +
  theme(plot.title = element_text(hjust=0.5), 
        legend.position="none",
        axis.text.x = element_text(margin = margin(t=-5)),
        axis.ticks = element_blank())
fig1a
dev.off()

pdf(file = "fig1b.pdf", width=4.75, height=4.75)
fig1b <- ggplot(freq_protest, aes(x = reorder(feature2, frequency_Both), 
                                  y = frequency_Both)) +
  geom_point() + 
  coord_flip() +
  ylim(0,300) +
  labs(x = NULL, y = "Frequency") +
  ggtitle("Lawless Protest Dictionary") +
  theme_hc() +
  theme(plot.title = element_text(hjust=0.5),
        legend.position="none",
        axis.text.x = element_text(margin = margin(t=-5)),
        axis.ticks = element_blank())
fig1b
dev.off()


#Figure 3 - Frame utilization at tweet level
#recreating Figure 3A
data <- data %>%
  mutate(frame = ifelse(democracy == 1, "insurrect",
                        ifelse(protestonly == 1, "protest", 
                               ifelse(neither == 1, "neither", NA))))

data$frame <- as.factor(data$frame)
data$frame <- factor(data$frame, levels=c('insurrect', 'protest', 'neither'))

#Figure 3A
data$democrat <- as.factor(data$democrat)
fig3a <- ggplot(data, aes(x = frame, fill = democrat)) +
  geom_bar() +
  labs(x = "Frame of Tweet", y = "Number of Tweets") + 
  ggtitle("Frame Utilization by Tweet") +
  scale_fill_manual(values=c('red', 'blue'), labels=c('Republicans', 'Democrats'), name="") +
  scale_x_discrete(labels = c('Attack on Democracy', 'Lawless Protest', 'Neither')) +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        legend.margin=margin(t=-2))
fig3a
ggsave("fig3a.png",
       width=1500,
       height=1500,
       units="px",
       dpi=300)

#Figure 3B
sums <- data %>%
  group_by(democrat, chamber) %>%
  summarise(insurrection = mean(demonly, na.rm = TRUE), 
            protest = mean(protestonly, na.rm = TRUE),
            both = mean(both, na.rm = TRUE),
            neither = mean(neither, na.rm = TRUE)) %>%
  pivot_longer(!democrat & !chamber, names_to = "frame", values_to = "proportion") %>%
  mutate(group = ifelse(democrat == 0 & chamber == "House", "House Rs",
                        ifelse(democrat == 0 & chamber == "Senate", "Senate Rs",
                               ifelse(democrat == 1 & chamber == "House", "House Ds", "Senate Ds"))))


sums$frame <- factor(sums$frame, levels=c('protest', 'neither', 'both', 'insurrection'))
sums$group <- as.factor(sums$group)

fig3b <- ggplot(sums, aes(fill = frame, y = proportion, x = group)) +
  geom_bar(position = "fill", stat = "identity") + 
  labs(x = "Posting Member Chamber and Party", y = "Proportion of All Tweets", fill= "Tweet Frame:") + 
  ggtitle("Proportion of Tweets in Each Frame") +
  scale_x_discrete(labels=c('House Ds', 'House Rs', 
                            'Senate Ds', 'Senate Rs')) +
  scale_fill_manual(values = c("#5c055c", "gray70", "#377d37", "#055305"), 
                    labels = c("Protest", "Neither", "Both", "Democracy")) +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        legend.margin=margin(t=-2),
        axis.title.x = element_text(margin = margin(t=5))) +
  coord_flip()
fig3b
ggsave("fig3b.png",
       width=1500,
       height=1500,
       units="px",
       dpi=300)


#Table S3 - Topics identified in tweets using neither frame
data <- data %>%
  mutate(neither_topic2 = case_when(
    neither_topic == "election generic" | neither_topic == "election legitimate" | neither_topic == "election objection" | neither_topic == "return" ~ "election",
    neither_topic == "generic condemn" | neither_topic == "trump" ~ "condemn",
    neither_topic == "safe" | neither_topic == "other update" ~ "safe",
    neither_topic == "external" | neither_topic == "unclear" | neither_topic == "off-topic" ~ "misc",
    TRUE ~ NA
  ))

neithers <- subset(data, neither == 1)

top <- neithers %>%
  group_by(neither_topic2, democrat) %>%
  summarise(n=n())

all <- neithers %>%
  group_by(neither_topic) %>%
  summarise(n=n())

dems <- subset(neithers, democrat == 1)
reps <- subset(neithers, democrat == 0)

d1 <- dems %>%
  group_by(neither_topic) %>%
  summarise(n=n())

r1 <- reps %>%
  group_by(neither_topic) %>%
  summarise(n=n())

all <- left_join(all, d1, by="neither_topic")
all <- left_join(all, r1, by="neither_topic")

colnames(all) <- c("neither_topic", "total", "d_total", "r_total")
all[is.na(all)] <- 0

all$d_pct <- all$d_total/all$total
all$r_pct <- all$r_total/all$total
print(all)
#exports the data as text file
sink('tableS3.txt')
all
sink()

#Figure 4 - Tweet usage over time
#Figure 4A
data <- data %>%
  mutate(all_topic = case_when(
    democracy == 1 ~ "insurrection",
    protestonly == 1 ~ "protest",
    neither == 1 ~ neither_topic2,
    TRUE ~ NA
  ))

overtime1 <- data  %>%
  group_by(time_bin, all_topic) %>%
  summarise(n = n()) %>%
  mutate(percentage = n / sum(n))


overtime1$all_topic <- fct_relevel(overtime1$all_topic,
                                   "misc", "election", "condemn", 
                                   "safe", "protest", "insurrection")


fig4a <- ggplot(overtime1, aes(x=time_bin, y=n, fill=all_topic)) +
  geom_area() +
  labs(x = "", y = "Number of Tweets", fill="") +
  ggtitle("All Tweets by Hour") +
  theme_bw() +
  scale_fill_manual(values = c("gray80", "gray60", "gray40", "gray20", "darkorchid4", "darkgreen"),
                    labels = c("Miscellaneous", "Floor Proceedings", "General Condemnation", "Status Update", "Lawless Protest", "Attack on Democracy")) +
  scale_x_continuous(breaks = 2:9,
                     labels = c("1pm-2pm\nCapitol barriers\nbreached", "2pm-3pm\nVP evacuated;\nAshli Babbitt shot", "3pm-4pm", "4pm-5pm\nTrump video\n'Go Home'", "5pm-6pm\nCrowd thins out", "6pm-7pm\nCapitol Police\n'All Clear'","7pm-8pm","8pm-9pm\nPence reopens\nthe Senate")) +
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        legend.margin=margin(t=-10),
        axis.title.x = element_text(margin = margin(t=5)))
fig4a
ggsave("fig4a.png",
       width=2400,
       height=1500,
       units="px",
       dpi=300)

#Figure 2 - Tweet frequency and frame utilization at member level

#Figure 2A
memdata$democrat <- as.factor(memdata$democrat)
fig2a <- ggplot(memdata, aes(x = mem_tweet_count, fill = democrat, color = democrat)) +
  geom_density(alpha=0.75) +
  scale_fill_manual(values = c('red', 'blue'), labels = c('Republicans', 'Democrats'), name="") +
  scale_color_manual(values = c('red', 'blue'), labels = c('Republicans', 'Democrats'), name="") +
  labs(x = "Number of Tweets Posted", y = "Density") +
  ggtitle("Tweet Frequency by Member") +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5),
        legend.position="bottom",
        legend.margin=margin(t=-2))
fig2a
ggsave("fig2a.png",
       width=1500,
       height=1500,
       units="px",
       dpi=300)


#Figure 2B
memdata$demever <- ifelse(memdata$dem_total_mem > 0, 1, 0)
memdata$protever <- ifelse(memdata$prot_total_mem > 0, 1, 0)
memdata$nonever <- ifelse(memdata$demever == 0 & memdata$protever == 0, 1, 0)
memdata$bothever <- ifelse(memdata$demever == 1 & memdata$protever == 1, 1, 0)
memdata$demonly <- ifelse(memdata$demever == 1 & memdata$protever == 0, 1, 0)
memdata$protonly <- ifelse(memdata$demever == 0 & memdata$protever == 1, 1, 0)

memdata <- memdata %>%
  mutate(member_frame = case_when(demonly == 1 ~ "insurrect",
                                  protonly == 1 ~ "protest",
                                  bothever == 1 ~ "both",
                                  nonever == 1 ~ "neither",
                                  TRUE ~ NA))

plotdata <- memdata
plotdata$demever <- as.factor(plotdata$demever)
plotdata$protever <- as.factor(plotdata$protever)
plotdata$nonever <- as.factor(plotdata$nonever)
plotdata$democrat <- as.factor(plotdata$democrat)
plotdata$framechoice <- as.factor(plotdata$member_frame)

plotdata$framechoice <- fct_relevel(plotdata$framechoice,
                                    "insurrect", "protest", "both",
                                    "neither")

fig2b <- ggplot(plotdata, aes(x = framechoice, fill = democrat)) +
  geom_bar() + 
  scale_fill_manual(values=c('red', 'blue'), labels=c('Republicans', 'Democrats'), name="") +
  scale_x_discrete(labels=c('Democracy\nTweets Only','Protest\nTweets Only', 'Democracy and\nProtest Tweets', 'Neither\nTweets')) +
  xlab("Category of Tweets Posted") +
  ylab("Number of Members") +
  ggtitle("Frame Utilization by Member") +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        legend.margin=margin(t=-2),
        axis.title.x = element_text(margin = margin(t=5)))
fig2b
ggsave("fig2b.png",
       width=1500,
       height=1500,
       units="px",
       dpi=300)


#Table S4 - Negative binomial model 
memdata$leader <- memdata$maj_leader + memdata$min_leader
memdata$trump_margin2 <- memdata$trump_margin/10
memdata$votemargin2 <- memdata$votemargin/10

memdata$lntweetpy <- log(memdata$user_tweet_peryear)

mem_all3a <- glm.nb(mem_tweet_count ~ lntweetpy +
                      democrat +
                      seniority + 
                      leader +
                      votemargin2 +
                      trump_margin2 +
                      chamber,
                    data = memdata)
sink('tableS4.txt')
summary(mem_all3a)
sink()


#calculating chi squared
mem_base <- glm.nb(mem_tweet_count ~ 1,
                   data = memdata)
anova(mem_base, mem_all3a, test = "Chisq")


#Figure 5 - Model results for number of tweets posted by member
#Figure 5A - Coefficient plot
coefs <- as.data.frame(mem_all3a$coefficients)
coefs$ests <- exp(coefs$`mem_all3a$coefficients`)
coefs$se <- sqrt(diag(vcov(mem_all3a)))
coefs$vars <- rownames(coefs)

colnames(coefs) <- c("coefs", "ests", "se", "vars")  

coefs$lb <- coefs$coefs - 1.96*coefs$se
coefs$ub <- coefs$coefs + 1.96*coefs$se

coefs <- coefs %>%
  filter(vars!="(Intercept)") %>%
  mutate(vars = recode(vars,
                       "lntweetpy" = "Tweets per Year",
                       "democrat1" = "Democrat",
                       "seniority" = "Seniority",
                       "leader" = "Leadership",
                       "votemargin2" = "Electoral Safety",
                       "trump_margin2" = "Trump Margin",
                       "chamberSenate" = "Senator"
  ))

coefs$vars <- fct_relevel(coefs$vars,
                          "Electoral Safety",
                          "Leadership",
                          "Seniority",
                          "Democrat",
                          "Senator",
                          "Trump Margin",
                          "Tweets per Year")


fig5a <- ggplot(coefs, aes(vars, coefs)) + 
  geom_hline(yintercept=0, lty=3, colour="grey50") +
  geom_errorbar(aes(ymin=lb, ymax=ub), width=0) +
  geom_point(size=2) +
  coord_flip() +
  ylim(-0.6,0.6) +
  xlab("") +
  ylab("Estimated Coefficients\n(with 95% confidence intervals)") +
  ggtitle("Negative Binomial Model Results") +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5),
        axis.ticks = element_blank(),
        legend.position = "none") 
fig5a
ggsave("fig5a.png",
       width=1500,
       height=1500,
       units="px",
       dpi=300)

mem.dems <- subset(memdata, memdata$democrat == 1)
mem.reps <- subset(memdata, memdata$democrat == 0)

#Figure 5B - nb predictions
ppdata.d <- with(mem.dems, data.frame(lntweetpy = median(lntweetpy, na.rm = TRUE),
                                      democrat = 1,
                                      seniority = median(seniority, na.rm = TRUE),
                                      leader = 0,
                                      chamber = "House",
                                      votemargin2 = median(votemargin2, na.rm = TRUE),
                                      trump_margin2 = seq(from=-6.5, to=1.5, by=.5)))
ppdata.r <- with(mem.reps, data.frame(lntweetpy = median(lntweetpy, na.rm = TRUE),
                                      democrat = 0,
                                      seniority = median(seniority, na.rm = TRUE),
                                      leader = 0,
                                      chamber = "House",
                                      votemargin2 = median(votemargin2, na.rm = TRUE),
                                      trump_margin2 = seq(from=-1.5, to=6.5, by=.5)))
ppdata <- rbind(ppdata.d, ppdata.r)
ppdata$democrat <- as.factor(ppdata$democrat)

ppdata <- cbind(ppdata, predict(mem_all3a, newdata = ppdata, se=TRUE, type = "response"))

ppdata$trump_margin2 <- ppdata$trump_margin2*10

fig5b <- ggplot(ppdata, aes(x=trump_margin2, y=fit, group=democrat)) +
  geom_ribbon(aes(ymin=fit-se.fit, ymax=fit+se.fit, fill = democrat), alpha = 0.2) +
  geom_line(aes(color = democrat), linewidth=1) +
  scale_color_manual(name = "",
                     values=c("red", "blue"),
                     labels=c("Republican","Democrat")) +
  scale_fill_manual(name = "",
                    values=c("red", "blue"),
                    labels=c("Republican","Democrat")) +
  scale_x_continuous(breaks = seq(-60, 60, by = 30)) +
  labs(x = "Trump Margin of Victory", y = "Predicted Number of Tweets") +
  ggtitle("Negative Binomial Model Predictions") +
  ylim(0,7) +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5),
        legend.margin=margin(t=-2),
        legend.position = "bottom")
fig5b
ggsave("fig5b.png",
       width=1500,
       height=1500,
       units="px",
       dpi=300)

#Table S5 - Multinomial logistic regression results for all tweets
#creating variables for ML models
data <- data %>%
  mutate(quotetweet = case_when(sourcetweet_type == "quoted" ~ 1,
                                TRUE ~ 0),
         replytweet = case_when(!is.na(in_reply_to_user_id) ~ 1,
                                TRUE ~ 0),
         thread = case_when(author_id == in_reply_to_user_id ~ 1, 
                            TRUE ~ 0),
         tweet_type = case_when(thread == 1 ~ 'Thread',
                                thread == 0 & replytweet == 1 ~ 'Reply',
                                quotetweet == 1 ~ 'Quote',
                                TRUE ~ 'Original'))

data$tweet_type <- as.factor(data$tweet_type)

data$leader <- data$maj_leader + data$min_leader
data$trump_margin2 <- data$trump_margin/10
data$votemargin2 <- data$votemargin/10


data <- data %>%
  mutate(h2 = case_when(time_bin == 2 ~ 1,
                        TRUE ~ 0),
         h3 = case_when(time_bin == 3 ~ 1,
                        TRUE ~ 0),
         h4 = case_when(time_bin == 4 ~ 1,
                        TRUE ~ 0),
         h5 = case_when(time_bin == 5 ~ 1,
                        TRUE ~ 0),
         h6 = case_when(time_bin == 6 ~ 1,
                        TRUE ~ 0),
         h7 = case_when(time_bin == 7 ~ 1,
                        TRUE ~ 0),
         h8 = case_when(time_bin == 8 ~ 1,
                        TRUE ~ 0),
         h9 = case_when(time_bin == 9 ~ 1,
                        TRUE ~ 0))


mldata <- mlogit.data(data, varying=NULL, choice="frame", shape="wide")

#this is the actual model in Table S5
mlmodel1b <- mlogit(frame ~ 1 | democrat + 
                      trump_margin2 +
                      factor(time_bin) + 
                      votemargin2 +
                      primary +
                      seniority +
                      leader +
                      tweet_type,
                    data = mldata, reflevel = "neither")
sink('tableS5.txt')
summary(mlmodel1b)
sink()


#Table S6 - Multinomial logistic regression results by party
#split ML data into partisan subgroups
data.dems <- subset(mldata, mldata$democrat == 1)
data.reps <- subset(mldata, mldata$democrat == 0)

#partisan ML models Democrats
#Table S6 Democratic Tweets
mlmodel2b <- mlogit(frame ~ 1 | trump_margin2 +
                      factor(time_bin) + 
                      votemargin2 +
                      primary +
                      seniority +
                      leader +
                      tweet_type,
                    data = data.dems, reflevel = "neither")
sink('tableS6_dem.txt')
summary(mlmodel2b)
sink()

#partisan ML models Republicans
#Table S6 Republican Tweets
mlmodel3b <- mlogit(frame ~ 1 | trump_margin2 +
                      factor(time_bin) + 
                      votemargin2 +
                      primary +
                      seniority +
                      leader +
                      tweet_type,
                    data = data.reps, reflevel = "neither")
sink('tableS6_rep.txt')
summary(mlmodel3b)
sink()

#same model but this version lets me make my predicted graph
mlmodel3b2 <- mlogit(frame ~ 1 | trump_margin2 +
                       h3+h4+h5+h6+h7+h8+h9+ 
                       votemargin2 +
                       primary +
                       seniority +
                       leader +
                       tweet_type,
                     data = data.reps, reflevel = "neither")

#Figure 6 - Model results for frame utilization by tweet
#Figure 6A - predicted frame by party
ppdata.ml <- predict(mlmodel1b, newdata = mldata, se=TRUE, type = "response")

ppdata.small <- data %>%
  select(frame, democrat, trump_margin2, time_bin, 
         votemargin, primary, seniority, leader, tweet_type)

ppdata.small <- na.omit(ppdata.small)

ppdata.ml <- cbind(ppdata.ml, ppdata.small)
ppdata.ml <- ppdata.ml %>%
  pivot_longer(cols = 1:3,
               names_to = "outcome",
               values_to = "fit")

ppdata.ml$outcome <- fct_relevel(ppdata.ml$outcome,
                                 "insurrect", "protest", "neither")

ppdata.ml$democrat <- as.factor(ppdata.ml$democrat)

fig6a <- ggplot(ppdata.ml, aes(x=outcome, y=fit, fill=democrat)) +
  geom_boxplot(aes(fill = democrat), alpha = .9,
               outlier.shape = NA) + 
  scale_fill_manual(name = "",
                    values=c("red", "blue"),
                    labels=c("Republican","Democrat")) +
  scale_x_discrete(breaks=c("insurrect", "protest", "neither"),
                   labels=c("Attack on Democracy", "Lawless Protest", "Neither")) +
  coord_cartesian(ylim = c(0,1)) +
  xlab("") +
  ylab("Probability of Frame") +
  ggtitle("Predicted Frame by Party") +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5),
        legend.margin=margin(t=-15),
        legend.position = "bottom")
fig6a
ggsave("fig6a.png",
       width=1500,
       height=1500,
       units="px",
       dpi=300)


#figure 6B - predicted frame for republican tweets
ppdata.r <- with(data.reps, data.frame(frame = c("democracy", "democracy","democracy","democracy",
                                                 "neither","neither","neither","neither","neither",
                                                 "protest","protest","protest","protest","protest",
                                                 "protest"), 
                                       trump_margin2 = seq(from=-1, to=6, by=.5),
                                       h3 = 0, h4 = 1, h5 = 0, h6 = 0, h7 = 0, h8 = 0, h9 = 0,
                                       votemargin2 = median(votemargin2, na.rm = TRUE),
                                       primary = median(primary, na.rm = TRUE),
                                       seniority = median(seniority, na.rm = TRUE),
                                       leader = 0,
                                       tweet_type = factor("Original", 
                                                           levels=c("Original", "Quote", "Reply", "Thread"))))


mlppdata.r <- mlogit.data(ppdata.r, varying=NULL, choice="frame", shape="wide")

ppdata.rep <- predict(mlmodel3b2, newdata = mlppdata.r, interval = "confidence", type = "response")

ppdata.rep <- cbind(ppdata.rep, ppdata.r)
ppdata.rep <- ppdata.rep %>%
  pivot_longer(cols = 1:3,
               names_to = "outcome",
               values_to = "fit")


ppdata.rep$outcome <- fct_relevel(ppdata.rep$outcome,
                                  "democracy", "protest", "neither")

#generating bootstrapped confidence intervals for predicted values
est_betas <- mlmodel3b2$coefficients
est_preds <- predict(mlmodel3b2, newdata = mlppdata.r)

est_ppdata.rep <- cbind(est_preds, ppdata.r)
est_ppdata.rep <- est_ppdata.rep %>%
  pivot_longer(cols = 1:3,
               names_to = "outcome",
               values_to = "fit")
est_ppdata.rep <- est_ppdata.rep %>%
  mutate(order = case_when(outcome == "neither" ~ 1,
                           outcome == "democracy" ~ 2,
                           TRUE ~ 3)) %>%
  arrange(order, trump_margin2)

set.seed(49)
sim_betas <- mvrnorm(1000, mlmodel3b2$coefficients, vcov(mlmodel3b2))
sim_preds <- apply(sim_betas, 1, function(x) {
  mlmodel3b2$coefficients <- x
  predict(mlmodel3b2, newdata = mlppdata.r)
})
sim_ci <- apply(sim_preds, 1, quantile, c(.025, .975))

est_ppdata.rep <- cbind(est_ppdata.rep, t(sim_ci))
est_ppdata.rep <- est_ppdata.rep %>% 
  rename(lb = '2.5%') %>%
  rename(ub = '97.5%')

est_ppdata.rep$trump_margin2 <- est_ppdata.rep$trump_margin2*10

fig6b <- ggplot(est_ppdata.rep, aes(x=trump_margin2, y=fit, group=outcome)) +
  geom_ribbon(aes(ymin=lb, ymax=ub, fill = outcome), alpha = 0.3) +
  geom_line(aes(color = outcome), linewidth=1) + 
  labs(x = "Trump Margin of Victory", y = "Probability of Frame Usage") +
  scale_color_manual(values = c("#055305", "gray70", "#5c005c"), 
                     labels = c("Attack on Democracy", "Neither", "Lawless Protest"),
                     name="") +
  scale_fill_manual(values = c("#055305", "gray70", "#5c005c"), 
                    labels = c("Attack on Democracy", "Neither", "Lawless Protest"),
                    name="") +
  ggtitle("Predicted Frame for Republican Tweets") +
  ylim(0,1) +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom")
fig6b
ggsave("fig6b.png",
       width=1500,
       height=1500,
       units="px",
       dpi=300)

#Coefficient plots for ML models
#Figure 6C - Democratic tweets
coefs.d <- as.data.frame(mlmodel2b$coefficients)
coefs.d$ests <- exp(coefs.d$`mlmodel2b$coefficients`)
coefs.d$se <- sqrt(diag(vcov(mlmodel2b)))
coefs.d$vars <- rownames(coefs.d)

colnames(coefs.d) <- c("coefs", "ests", "se", "vars")  

coefs.d$lb <- exp(coefs.d$coefs - 1.96*coefs.d$se)
coefs.d$ub <- exp(coefs.d$coefs + 1.96*coefs.d$se)

coefs.d$model <- "Democrats"

coefs.d <- 
  coefs.d %>% separate_wider_delim(vars, delim = ":", names = c("var", "frame"))


coefs.d <- coefs.d %>%
  filter(var!="(Intercept)" & 
           var!= "tweet_typeQuote" &
           var!= "tweet_typeReply" &
           var!= "tweet_typeThread" &
           var!= "factor(time_bin)3" &
           var!= "factor(time_bin)4" &
           var!= "factor(time_bin)5" &
           var!= "factor(time_bin)6" &
           var!= "factor(time_bin)7" &
           var!= "factor(time_bin)8" &
           var!= "factor(time_bin)9") %>%
  mutate(var = recode(var,
                      "trump_margin2" = "Trump Margin",
                      "votemargin2" = "Electoral Safety",
                      "primary" = "Primary",
                      "seniority" = "Seniority",
                      "leader" = "Leadership",
  ))

coefs.d$var <- fct_relevel(coefs.d$var,
                           "Electoral Safety",
                           "Primary",
                           "Leadership",
                           "Seniority",
                           "Trump Margin")


fig6c <- ggplot(coefs.d, aes(var, ests, color=frame)) + 
  geom_hline(yintercept=1, lty=3, colour="grey50") +
  geom_errorbar(aes(ymin=lb, ymax=ub), width=0,
                position=position_dodge(width=0.3)) +
  geom_point(size=2,
             position=position_dodge(width=0.3)) +
  scale_color_manual(name = "",
                     values = c("#377d37", "#5c055c"),
                     labels = c("Attack on Democracy", "Lawless Protest")) +
  coord_flip() +
  ylim(0,3.7) +
  xlab("") +
  ylab("Estimated Odds Ratios") +
  ggtitle("Multinomial Logit Model Results - Democrats") +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5),
        axis.ticks = element_blank(),
        legend.position = c(0.75, 0.13),
        legend.title = element_blank())
fig6c
ggsave("fig6c.png",
       width=1500,
       height=1500,
       units="px",
       dpi=300)

#Figure 6D - Republican tweets
coefs.r <- as.data.frame(mlmodel3b$coefficients)
coefs.r$ests <- exp(coefs.r$`mlmodel3b$coefficients`)
coefs.r$se <- sqrt(diag(vcov(mlmodel3b)))
coefs.r$vars <- rownames(coefs.r)

colnames(coefs.r) <- c("coefs", "ests", "se", "vars")  

coefs.r$lb <- exp(coefs.r$coefs - 1.96*coefs.r$se)
coefs.r$ub <- exp(coefs.r$coefs + 1.96*coefs.r$se)

coefs.r$model <- "Republicans"

coefs.r <- 
  coefs.r %>% separate_wider_delim(vars, delim = ":", names = c("var", "frame"))


coefs.r <- coefs.r %>%
  filter(var!="(Intercept)" & 
           var!= "tweet_typeQuote" &
           var!= "tweet_typeReply" &
           var!= "tweet_typeThread" & 
           var!= "factor(time_bin)3" &
           var!= "factor(time_bin)4" &
           var!= "factor(time_bin)5" &
           var!= "factor(time_bin)6" &
           var!= "factor(time_bin)7" &
           var!= "factor(time_bin)8" &
           var!= "factor(time_bin)9") %>%
  mutate(var = recode(var,
                      "trump_margin2" = "Trump Margin",
                      "votemargin2" = "Electoral Safety",
                      "primary" = "Primary",
                      "seniority" = "Seniority",
                      "leader" = "Leadership",
  ))

coefs.r$var <- fct_relevel(coefs.d$var,
                           "Electoral Safety",
                           "Primary",
                           "Leadership",
                           "Seniority",
                           "Trump Margin")


fig6d <- ggplot(coefs.r, aes(var, ests, color=frame)) + 
  geom_hline(yintercept=1, lty=3, colour="grey50") +
  geom_errorbar(aes(ymin=lb, ymax=ub), width=0,
                position=position_dodge(width=0.3)) +
  geom_point(size=2,
             position=position_dodge(width=0.3)) +
  scale_color_manual(name = "",
                     values = c("#377d37", "#5c055c"),
                     labels = c("Attack on Democracy", "Lawless Protest")) +
  coord_flip() +
  ylim(0,3.7) +
  xlab("") +
  ylab("Estimated Odds Ratios") +
  ggtitle("Multinomial Logit Model Results - Republicans") +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5),
        axis.ticks = element_blank(),
        legend.position = c(0.75, 0.13),
        legend.title = element_blank())
fig6d
ggsave("fig6d.png",
       width=1500,
       height=1500,
       units="px",
       dpi=300)


#Table S7 - 2022 electoral outcomes by party and frame usage
electdata <- read.csv("nextelection.csv")

memdata <- memdata %>%
  left_join(electdata, by="icpsr")

memdata <- memdata %>%
  mutate(reelected = case_when(next_prim_lose == 0 & next_retire_elect == 0 & 
                                 next_retire == 0 & next_lose_gen == 0 & 
                                 next_other == 0 ~ 1,
                               next_prim_lose == 1 | next_retire_elect == 1 |
                                 next_retire == 1 | next_lose_gen == 1 |
                                 next_other == 1 ~ 0,
                               TRUE ~ NA))

memdata <- memdata %>%
  mutate(elect_outcome = case_when(reelected == 1 ~ "reelected",
                                   next_lose_gen == 1 ~ "lost general",
                                   next_prim_lose == 1 ~ "lost primary",
                                   next_retire_elect == 1 ~ "other office",
                                   next_retire == 1 ~ "retired",
                                   TRUE ~ NA))

memdata <- memdata %>%
  mutate(elect_outcome2 = case_when(reelected == 1 ~ "reelected",
                                    next_lose_gen == 1 ~ "lost",
                                    next_prim_lose == 1 ~ "lost",
                                    next_retire_elect == 1 ~ "retired",
                                    next_retire == 1 ~ "retired",
                                    TRUE ~ NA))



memdata$framechoice <- fct_relevel(memdata$member_frame,
                                      "insurrect", "protest", "both", "neither")


memdata.el <- subset(memdata, next_elect == 2022)    
memdata.el <- subset(memdata.el, next_other == 0)

sink('tableS7.txt')
memdata.el %>%
  group_by(democrat, framechoice) %>%
  summarise(n = n(),
            pchal = mean(next_prim_chal, na.rm = TRUE),
            ploss = mean(next_prim_lose, na.rm = TRUE),
            pvote = mean(next_prim_vote, na.rm = TRUE),
            pcand = mean(next_prim_cand, na.rm = TRUE),
            ret = mean(next_retire, na.rm = TRUE),
            ran_other = mean(next_retire_elect, na.rm = TRUE),
            gloss = mean(next_lose_gen, na.rm = TRUE),
            reelect = mean(reelected, na.rm = TRUE))
sink()


mem.el.r <- subset(memdata.el, democrat == 0)
mem.el.d <- subset(memdata.el, democrat == 1)

chisq.test(mem.el.r$next_prim_chal, mem.el.r$framechoice)
chisq.test(mem.el.r$next_prim_lose, mem.el.r$framechoice)
res.aov <- aov(next_prim_vote ~ framechoice, data = mem.el.r)
summary(res.aov)
res.aov <- aov(next_prim_cand ~ framechoice, data = mem.el.r)
summary(res.aov)
chisq.test(mem.el.r$next_retire, mem.el.r$framechoice)
chisq.test(mem.el.r$next_retire_elect, mem.el.r$framechoice)
chisq.test(mem.el.r$next_lose_gen, mem.el.r$framechoice)
chisq.test(mem.el.r$reelected, mem.el.r$framechoice)

chisq.test(mem.el.d$next_prim_chal, mem.el.d$framechoice)
chisq.test(mem.el.d$next_prim_lose, mem.el.d$framechoice)
res.aov <- aov(next_prim_vote ~ framechoice, data = mem.el.d)
summary(res.aov)
res.aov <- aov(next_prim_cand ~ framechoice, data = mem.el.d)
summary(res.aov)
chisq.test(mem.el.d$next_retire, mem.el.d$framechoice)
chisq.test(mem.el.d$next_retire_elect, mem.el.d$framechoice)
chisq.test(mem.el.d$next_lose_gen, mem.el.d$framechoice)
chisq.test(mem.el.d$reelected, mem.el.d$framechoice)


#Table S8 - House Republicans casting pro-Trump votes by frame usage
repvotes <- read.csv("repvotes.csv")

repvotes <- repvotes %>%
  select(icpsr, vote_1_AZ_electors, vote_2_PA_electors, vote_3_impeachment,
         vote_4_national_commission)

memdata <- left_join(memdata, repvotes, by="icpsr")

memdata <- memdata %>%
  mutate(az_vote = case_when(vote_1_AZ_electors == 1 ~ 1,
                             vote_1_AZ_electors == -1 | vote_1_AZ_electors == 0 ~ 0,
                             TRUE ~ NA)) %>%
  mutate(pa_vote = case_when(vote_2_PA_electors == 1 ~ 1,
                             vote_2_PA_electors == -1 | vote_2_PA_electors == 0 ~ 0,
                             TRUE ~ NA)) %>%
  mutate(imp_vote = case_when(vote_3_impeachment == 1 ~ 1,
                              vote_3_impeachment == -1 | vote_3_impeachment == 0 ~ 0,
                              TRUE ~ NA)) %>%
  mutate(comm_vote = case_when(vote_4_national_commission == 1 ~ 1,
                               vote_4_national_commission == -1 | vote_4_national_commission == 0 ~ 0,
                               TRUE ~ NA)) %>%
  mutate(all_votes = case_when(az_vote == 1 & pa_vote == 1 & imp_vote == 1 & comm_vote == 1 ~ 1,
                               democrat == 1 ~ NA,
                               chamber == "Senate" ~ NA,
                               TRUE ~ 0))

sink('tableS8.txt')
memdata %>%
  group_by(democrat, framechoice) %>%
  summarise(az.vot = mean(az_vote, na.rm = TRUE),
            pa.vot = mean(pa_vote, na.rm = TRUE),
            imp.vot = mean(imp_vote, na.rm = TRUE),
            comm.vot = mean(comm_vote, na.rm = TRUE),
            all.votes = mean(all_votes, na.rm = TRUE))
sink()

chisq.test(memdata$az_vote, memdata$framechoice)
chisq.test(memdata$pa_vote, memdata$framechoice)
chisq.test(memdata$imp_vote, memdata$framechoice)
chisq.test(memdata$comm_vote, memdata$framechoice)
chisq.test(memdata$all_votes, memdata$framechoice)
